package com.example.controller;

import com.example.common.Result;
import com.example.entity.Category;
import com.example.entity.Menu;
import com.example.entity.PageBean;
import com.example.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/categories")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    // 创建新Category
//    @PostMapping
//    public Category createCategory(@RequestBody Category category) {
//        return categoryService.createCategory(category);
//    }

    // 获取所有Categories
    @GetMapping
    public Result getAllCategories() {

//        return categoryService.findAllCategories();
        return Result.success(categoryService.findAllCategories());
    }

    @GetMapping("/getCategories")
    public Result getCategories() {
        return Result.success(categoryService.findCategories());
    }
    @GetMapping("/page")
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize, String name
    ) {
        PageBean pageBean = categoryService.page(page, pageSize, name);
        return Result.success(pageBean);
    }


    @PostMapping("/add")
    public Result add(@RequestBody Category category) {
        categoryService.add(category);
        return Result.success();
    }

    @DeleteMapping("/delete")
    public Result delete(@RequestParam("id") Integer id) {
        categoryService.delete(id);
        return Result.success();
    }

    @PutMapping("/updata")
    public Result updata(@RequestBody Category category){
        categoryService.updata(category);
        return Result.success();
    }
}
